package webjoch.process;

import org.rsbuddy.tabs.Inventory;

import com.rsbuddy.script.methods.Calculations;
import com.rsbuddy.script.methods.Mouse;
import com.rsbuddy.script.methods.Npcs;
import com.rsbuddy.script.task.Task;

import webjoch.Main;
import webjoch.methods.Items;
import webjoch.methods.Links;
import webjoch.methods.Store;
import webjoch.misc.Constants;
import webjoch.process.strategies.Combat;
import webjoch.process.strategies.Puzzle;
import webjoch.wrappers.Door;
import webjoch.wrappers.Room;
/**
 * Clear the room
 * @author Webjoch
 *
 */
public class ProcessRoom {
	private Room room;
	
	public ProcessRoom (Room room){
		this.room = room;
	}
	
	public boolean loop(){
		webjoch.wrappers.Task thisProcess = new webjoch.wrappers.Task(room);
		Main.getDungeon().addTask(thisProcess);
		
		if (room.getOpenDoorCount() > Links.getRoomLinks(room).length){
			room.updateLinks();
		}
		if (Main.ignoreCurrentRoom) return true;
		/*if (room.getOpenDoorCount() == room.getDoorCount() && room.getKey() == null && room.getType() != Room.Type.BASE && SolvedPuzzle.getPuzzle() == null){
			Main.getDungeon().removeTask(thisProcess, true);
			return true;
		}*/
		
		webjoch.wrappers.Task task;
		for (Door door : room.getDoors()){
			if (door.getType() == Door.Type.SKILL && !door.isOpen() && door.getNeededSkillItem()[0] != 0 && !Inventory.containsOneOf(door.getNeededSkillItem())){
				task = new webjoch.wrappers.Task(room);
				task.setItems(door.getNeededSkillItem());
				Main.getDungeon().removeTask(task, false);
				Main.getDungeon().addTask(task);
			}
		}
		
		Puzzle puzzle = room.getPuzzle();
		if (room.getType() != Room.Type.PUZZLE || puzzle.isFinished() || !puzzle.isFinished() && !puzzle.hasPriority()){
			if (room.containsEnemies() && (!Main.shouldRush() || Inventory.getCount(true, Constants.COINS) < 437 || room.isGuardianRoom() || room.getType() == Room.Type.PUZZLE) && room.getDoorCount() > 1){
				Main.setStatus("Kill enemies");
				Combat.killEnemies(room.getEnemies());
				return false;
			}
		}
		
		if (room.getType() == Room.Type.PUZZLE && !puzzle.isFinished()){
			if (room.getPuzzle().itemNeed().length != 0 && !Inventory.containsAll(room.getPuzzle().itemNeed())){
				task = new webjoch.wrappers.Task(room);
				task.setItems(room.getPuzzle().itemNeed());
				Main.getDungeon().removeTask(task, false);
				Main.getDungeon().addTask(task);
			}else{
				Main.setStatus("solve puzzle ("+ puzzle.getType()+")");
				puzzle.loop();
				return false;
			}
		}
		
		if (Items.getGroundItems()){
			Main.setStatus("Get GroundItems");
			return false;
		}
		
		int neededItems = 0;
		for (webjoch.wrappers.Task task1 : Main.getDungeon().getTasks()){
			if (task1.getNeededItems()[0] != 0 && !Inventory.containsAll(task1.getNeededItems())) neededItems++;
		}
		if (room.getType() == Room.Type.BASE && room.inRoom() && neededItems > 0 && Inventory.getCount(true, Constants.COINS) >= 437){
			Main.setStatus("Trade with smugler");
			buyItems();
			return false;
		}
		
		if (room.inRoom()) Main.getDungeon().removeTask(thisProcess, room.getOpenDoorCount() == room.getDoorCount());
		return true;
	}
	
	/**
	 * Buy items from Smugler that needed for the open tasks
	 */
	public void buyItems(){
		if (Store.isOpen()){
			for (webjoch.wrappers.Task task : Main.getDungeon().getTasks()){
				if (task.getNeededItems()[0] == 0) continue;
				for (int itemId : task.getNeededItems()){
					if (!Inventory.contains(itemId)) Store.buy(itemId, 1);
				}
			}
			Store.close();
		}else{
			if (Calculations.distanceTo(Npcs.getNearest(Constants.SMUGLER)) > 5){
				Npcs.getNearest(Constants.SMUGLER).getLocation().clickOnMap();
			}else{
				Npcs.getNearest(Constants.SMUGLER).interact("Trade");
				for (int i = 0; i < 100; i++){
					if (Store.isOpen()){
						Task.sleep(1000,2000);
						Store.getWidget().getComponent(24).hover();
						for (int i2 = 0; i2 < 10; i2++){
							Mouse.scroll(false);
						}
						break;
					}
					Task.sleep(50);
				}
			}
		}
	}
}
